package cn.tedu._05mvcboot02.controller;

import cn.tedu._05mvcboot02.comment.response.JsonResult;
import cn.tedu._05mvcboot02.mapper.UserMapper;
import cn.tedu._05mvcboot02.pojo.dto.InsertDTO1;
import cn.tedu._05mvcboot02.pojo.entity.User;
import cn.tedu._05mvcboot02.pojo.vo.UserListVO;
import cn.tedu._05mvcboot02.pojo.vo.UserVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

//@Controller

@Slf4j
@RestController
@Api(tags = "用户模块")
@RequestMapping("/v1/users")
public class UserController {
    @Autowired
    private UserMapper userMapper;

    @PostMapping("insert")
    @ApiOperation(value = "插入用户信息功能")
    public JsonResult insert(InsertDTO1 insertDTO1){
        User user = new User();
        BeanUtils.copyProperties(insertDTO1,user);
        user.setCreated(new Date());
        userMapper.insert(user);
        return JsonResult.ok();
    }

    @GetMapping("userList")
    @ApiOperation(value = "查询用户信息功能")
    public JsonResult userList(){
        List<UserListVO> userListVOS = userMapper.userList();
        return JsonResult.ok(userListVOS);
    }
    @GetMapping("delete")
    @ApiOperation(value = "根据id删除用户信息功能")
    @ApiImplicitParam(name = "id",value = "用户id",required = true)
    public JsonResult deleteById(Integer id){
        userMapper.deleteById(id);
        return JsonResult.ok();
    }
    @PostMapping("update")
    @ApiOperation(value = "修改用户信息功能")
    @ApiImplicitParam(name = "user",value = "用户名",required = true)
    public JsonResult update(User user){
        userMapper.update(user);
        return JsonResult.ok();
    }

    @GetMapping("select/{id}")
    @ApiOperation(value = "根据id查询用户信息功能)")
    @ApiImplicitParam(name = "id",value = "用户id",required = true)
    private JsonResult select(@PathVariable Integer id){
        log.debug("id = " + id);
        UserVO userVO = userMapper.select(id);
        if(id < 0){
            throw new IllegalArgumentException("用户编号不能小于零");
        }
        return JsonResult.ok(userVO);
    }
}
